Ext.namespace("Ext.sw");

Ext.sw.ImageWin = function(config){
	config = config || {};
	if(config.src){
		this.src = config.src;
	}
	if(config.defSrc){
		this.defSrc = config.defSrc;
	}
	Ext.sw.ImageWin.superclass.constructor.apply(this,arguments);
}
Ext.extend(Ext.sw.ImageWin,Ext.Window,{
		title : '图片显示',
		width : 400,
		height : 300,
		bodyStyle : 'background-color:#E5E3DF;',
		resizable : false,
		closeAction:'hide',
		img : null,
		src : null,
		defSrc : null,
		lazyLoad:true,
		hasLoad:false,
		initComponent : function(){
			if(!this.lazyLoad){
				this.buildImg();
			}
			Ext.sw.ImageWin.superclass.initComponent.call(this);
		},
		
		buildImg:function(){
			if(!this.hasLoad){
				if(!this.loadImg()){
					this.loadDefImg();
				}
			}
		},
		
		loadImg:function(){
			if(this.img == null && this.src != null){
				this.img = new Ext.sw.component.Image({src:this.src});
				this.removeAll();
				this.add(this.img);
				this.hasLoad = true;
				return true;
			}else if(this.src && this.img.el){
				this.hasLoad = true;
				return true;
			}
			return false;
		},
		
		loadDefImg:function(){
			if(this.defSrc && this.img == null){
				this.img = new Ext.sw.component.Image({src:this.defSrc});
				this.removeAll();
				this.add(this.img);
				this.hasLoad = true;
				return true;
			}else if(this.img.el && this.defSrc){
				this.img.el.dom.src = this.defSrc;
				this.hasLoad = true;
				return true;
			}
			return false;
		},
		onRender: function() {
			this.buildImg();
			Ext.sw.ImageWin.superclass.onRender.apply(this, arguments);
		},
		
		afterRender:function(options){
		  Ext.sw.ImageWin.superclass.afterRender.apply(this,arguments);
		  if(this.rendered && this.img){
		  	//this.pageInit();
		  }
		},
		doLayout:function(opt){
			//console.log(this.rendered);
			if(this.rendered && !opt){
				new Ext.util.DelayedTask(this.pageInit,this).delay(100); 
			}else if(this.rendered && opt){
				this.pageInit();
			}
		},
		setDefSrc:function(srcs){
			this.hasLoad = false;
			this.defSrc = srcs;
			this.img = null;
			this.resetWin();
		},
		
		resetWin:function(){
			this.buildImg();
			this.doLayout(false);
		},
		
		resetWinNow:function(){
			this.buildImg();
			this.doLayout(true);
		},
		
		setSrc:function(srcs){
			this.hasLoad = false;
			this.src = srcs;
			if(this.el){
				this.img.el.dom.src=srcs;
			}
			this.resetWin();
		},
		
		pageInit : function(){
			var image = this.img.el;
			if(image == null){
				return;
			}
			image.setStyle('display','block');
			
			image.center();//图片居中
			image.setStyle("top",0);
			image.setStyle("left",0);
			//console.log("load:"+image.dom.complete);
			//获得原始尺寸
			image.osize = {
				width:image.getWidth() == 0?400:image.getWidth(),
				height:image.getHeight() == 0?300:image.getHeight()
			};
			//console.log("width:"+image.getWidth()+" height:"+image.getHeight());
			if(!this.checkImgDomExits(image) && this.defSrc){
				this.loadDefImg();
				this.resetWinNow();
			}
			this.setWidth(image.osize.width+15);
			this.setHeight(image.osize.height+33);
		},
		checkImgDomExits:function(image){
			return image.getWidth() > 30 && image.getHeight() > 30;
		}
});
Ext.reg('imgWin', Ext.sw.ImageWin);